package com.ofek.applicationbase.forms.question;

import java.io.Serializable;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.ofek.applicationbase.R;
import com.ofek.applicationbase.forms.QuestionView;


public class OptionBoxQuestionView extends QuestionView {

	private Spinner optionsBox;
	private QuestionOption[] options;
	protected int resId;
	private TextView disabled;
	private TextView question;
	private Object defaultAnswer;

	public <T> OptionBoxQuestionView(final int resourceId,final Context context,final String... options) {
		super(R.layout.form_question_optionbox,context);
		init(resId = resourceId);
		setOptions(options);
	}
	public <T> OptionBoxQuestionView(final int resourceId,final Context context,final QuestionOption... options) {
		super(R.layout.form_question_optionbox,context);
		init(resId = resourceId);
		setOptions(options);
	}
	
	private void init(final int res) {
		optionsBox = (Spinner)findViewById(R.id.optionsbox);
		optionsBox.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//requestLayout();
				if (event.getAction()!=MotionEvent.ACTION_UP) return false;
				if (defaultAnswer!=null && getAnswer()==null)  {
					for (int i= 0 ; i < options.length; i++) 
						if (defaultAnswer.equals(options[i].toAnswer())) {
							final int j = i ;
							optionsBox.setSelection(j,true);
							if (getAnswer()!=null) disabled.setText(getAnswer().toString());
							return false;
						}
				}
				return false;
			}
		});
		optionsBox.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(final AdapterView<?> parent, final View view,final int position, final long id) {
				broadcastAnswerChanged(options[position].toAnswer());
			}

			@Override
			public void onNothingSelected(final AdapterView<?> parent) {
			}
		});
		question = (TextView)findViewById(R.id.question);
		question.setText(getContext().getString(resId)+(isRequired() ? "*" : ""));
		disabled = (TextView)findViewById(R.id.disabled);
	}
	
	@Override
	public void setRequired(boolean required) {
		super.setRequired(required);
		question.setText(getContext().getString(resId)+(isRequired() ? "*" : ""));
	}
	@Override
	public Serializable getAnswer() {
		final int selectedItemPosition = optionsBox.getSelectedItemPosition();
		if (selectedItemPosition>=0) 
			return (Serializable)options[selectedItemPosition].toAnswer();
		return null;
	}
	@Override
	public void setAnswer(final Object defaultAnswer) {
		if (defaultAnswer==null) return;
		for (int i= 0 ; i < options.length; i++) 
			if (defaultAnswer.equals(options[i].toAnswer())) {
				optionsBox.setSelection(i);
				disabled.setText(getAnswer().toString());
				return;
			}
		
	}
	public void setOptions(final QuestionOption... options) {
		this.options = options;
		final String[] strings = new String[options.length];
		for (int i = 0 ; i < options.length ;i++) {
			strings[i]=options[i].toString();
		}
			
		final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(),
			R.layout.spinner_item, strings);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		optionsBox.setAdapter(dataAdapter);
	}
	public void setOptions(final String... options) {
		this.options = new QuestionOption[options.length];
		for (int i = 0 ; i < options.length ;i++) {
			this.options[i]=new SimpleQuestionOption(options[i]);
		}
		final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(),
			R.layout.spinner_item, options);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		optionsBox.setAdapter(dataAdapter);
	}
	
	public String[] getOptions() {
		final String[] strings = new String[options.length];
		for (int i = 0 ; i < options.length ;i++) {
			strings[i]=options[i].toString();
		}
		return strings;
	}
	public static class SimpleQuestionOption extends QuestionOption {
		private final String string;
		public SimpleQuestionOption(final String s) {
			string=s;
		}
		@Override
		public String toString() {
			return string;
		}
		
		@Override
		public Object toAnswer() {
			return string;
		}
	}
	@Override
	public void animateInvalidInput() {
		question.setTextColor(Color.RED);
		question.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
	}

	@Override
	protected void broadcastAnswerChanged(Object newAnswer) {
		if (hasValidInput()) 
			question.setTextColor(Color.BLACK);
		super.broadcastAnswerChanged(newAnswer);
	}
	@Override
	public void setEditable(final boolean edit) {
		if (edit) {
			optionsBox.setVisibility(View.VISIBLE);
			disabled.setVisibility(View.GONE);
		} else {
			optionsBox.setVisibility(View.GONE);
			disabled.setVisibility(View.VISIBLE);
			disabled.setText((String)getAnswer());
		}
		
	}
	@Override
	public void setDefaultAnswer(Object defaultAnswer, boolean shouldPrePick) {
		if (defaultAnswer!=null && shouldPrePick) setAnswer(defaultAnswer);
		this.defaultAnswer = defaultAnswer;
		
	}
}
